//
//  SettingViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/14.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "SettingViewController.h"
#import "CertificateDetailTopTableViewCell.h"
#import "CertificateDetailBottomTableViewCell.h"
#import "ApplyForHelpViewController.h"
#import "UploadDataViewController.h"
#import "QuitView.h"
#import "AboutAPPViewController.h"
#import "NormalQuestionViewController.h"
@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)NSMutableArray * cellData;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 42/WIDTH_6_SCALE;
        [_tableView registerClass:[CertificateDetailTopTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CertificateDetailTopTableViewCell class])];
        [_tableView registerClass:[CertificateDetailBottomTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CertificateDetailBottomTableViewCell class])];
        _tableView.backgroundColor = getColor(@"f5f5f5");
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = 42/WIDTH_6_SCALE;
    }return _tableView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[@[@{@"title":@"申请互助", @"content":@""},
                        @{@"title":@"上传资料", @"content":@""}],
                      @[@{@"title":@"关于博帮", @"content":@""},
                        @{@"title":@"常见问题", @"content":@""},
                        @{@"title":@"联系客服", @"content":@"400-888-8888"},
                        @{@"title":@"当前版本", @"content":@""},
                        @{@"title":@"退出登录", @"content":@""}]].mutableCopy;
    }return _cellData;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.section == 0) {
        CertificateDetailBottomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CertificateDetailBottomTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.cellData[indexPath.section][indexPath.row];
        return cell;
    }else{
        if (indexPath.row == 2 || indexPath.row == 3) {
            CertificateDetailTopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CertificateDetailTopTableViewCell class]) forIndexPath:indexPath];
            cell.model = self.cellData[indexPath.section][indexPath.row];
            return cell;
        }else{
            CertificateDetailBottomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CertificateDetailBottomTableViewCell class]) forIndexPath:indexPath];
            cell.model = self.cellData[indexPath.section][indexPath.row];
            if (indexPath.row == [self.cellData[indexPath.section] count] - 1) {
                cell.lineView.hidden = YES;
            }
            return cell;
        }
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.cellData[section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cellData.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10/WIDTH_6_SCALE)];
    footerView.backgroundColor = getColor(@"f5f5f5");
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10/WIDTH_6_SCALE;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            ApplyForHelpViewController * vc = [[ApplyForHelpViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 1) {
            UploadDataViewController * vc = [[UploadDataViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        if (indexPath.row == 0) {
            AboutAPPViewController * vc = [[AboutAPPViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 1) {
            NormalQuestionViewController * vc = [[NormalQuestionViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 2) {
            [self callPhone:@"400-888-8888"];
        }
        if (indexPath.row == 4) {
            QuitView * quitView = [[QuitView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
            [quitView show];
            quitView.quitLogBlock = ^{
                int64_t delayInSeconds = 2.0;      // 延迟的时间
                /*
                 *@parameter 1,时间参照，从此刻开始计时
                 *@parameter 2,延时多久，此处为秒级，还有纳秒等。10ull * NSEC_PER_MSEC
                 */
                [quitView cancelBtnClick];
                [self showprogressHUD];
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    // do something
                    [self hiddenProgressHUD];
                });
            };
        }
    }
}
@end
